package nio.Abuffer;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 帅小伙
 * @date 2022/1/14
 * @description
 * 操作系统直接对内存进行修改
 */
public class MappedByteBufferTest {

    public static void main(String[] args) throws Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile("file1.txt", "rw");

        //获取对应的文件通道
        FileChannel channel = randomAccessFile.getChannel();

        /*
        * 参数1 FileChannel.MapMode.READ_WRITE:读写模式
        * 参数2:起始位置
        * 参数3:映射到内存的大小 文件多少个字节映射到内存
        * mappedByteBuffer 实际类型是  DirectByteBuffer
        * */
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);

        mappedByteBuffer.put(0,(byte)'H');
        mappedByteBuffer.put(3,(byte)'9');

        randomAccessFile.close();

        System.out.println("修改成功");

    }
}
